package 
{
	
	import com.omniture.testandtarget.flashutils.Flashbox;
	import com.omniture.testandtarget.flashutils.FlashboxConnection;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.external.ExternalInterface;
	
	import org.osmf.containers.MediaContainer;
	import org.osmf.elements.ImageElement;
	import org.osmf.events.DisplayObjectEvent;
	import org.osmf.events.TimeEvent;
	import org.osmf.layout.LayoutMetadata;
	import org.osmf.layout.ScaleMode;
	import org.osmf.media.DefaultMediaFactory;
	import org.osmf.media.MediaElement;
	import org.osmf.media.MediaPlayer;
	import org.osmf.traits.LoadTrait;
	import org.osmf.traits.MediaTraitType;
	
	//Sets the size of the SWF
	[SWF(width="800", height="600")]
	
	public class WT04_IntegratingUIControls extends MovieClip
	{
		import org.osmf.elements.VideoElement;
		import org.osmf.media.URLResource;
		import org.osmf.utils.URL;
		
		////////////////////////////////////////////////////
		//DECLARATIONS
		////////////////////////////////////////////////////
		
		//URI of the media
		public static const PROGRESSIVE_PATH:String = "http://mediapm.edgesuite.net/strobe/content/test/AFaerysTale_sylviaApostol_640_500_short.flv";
		public static const STREAMING_PATH:String = "rtmp://cp67126.edgefcs.net/ondemand/mediapm/osmf/content/test/akamai_10_year_f8_512K";
		public static const STREAMING_MP4_PATH:String = "rtmp://cp67126.edgefcs.net/ondemand/mp4:mediapm/ovp/content/demo/video/elephants_dream/elephants_dream_768x428_24.0fps_408kbps.mp4";
		public static const LOCAL_SWF:String = "assets/swf_banner.swf";
		public static const DYNAMIC_STREAMING:String = "http://mediapm.edgesuite.net/osmf/content/test/manifest-files/dynamic_Streaming.f4m";
		
		public var player:MediaPlayer;
		public var container:MediaContainer;
		public var mediaFactory:DefaultMediaFactory;
		
		
		//control bar declarations
		public var controlBar:Sprite;
		public var controlBG:ControlBG;
		public var pauseButton:PauseButton;
		public var playButton:PlayButton;
		public var progressBG:ProgressBG;
		public var progressCurrent:ProgressCurrent;
		
		////////////////////////////////////////////////////
		//CONSTRUCTOR
		////////////////////////////////////////////////////
		
		public function WT04_IntegratingUIControls()
		{
			flashboxInit();
			
			//initPlayer();
			
			//Marker 2: Initialze Control Bar
			initControlBar();
		}
		
		
		 function flashboxInit():void{
			// USER CONFIG SETTINGS
			var clientCode:String = "larrytucker";
			var flashBox:String = "OSMF2"; 
			var logo_overlay:String = "";	
			var logo_x:String = "";
			var logo_y:String = "";			
			// END USER CONFIG SETTINGS
			
			// DEFAULT CONTENT
			var defaultContent:XML = 
				<offer>
					<logo_overlay>image1</logo_overlay>
					<logo_x>10</logo_x>
					<logo_y>20</logo_y>
				</offer>;	
			
				
			var myConnection:FlashboxConnection = new FlashboxConnection(clientCode); 
			// Create the Flashbox with the desired flashbox name.	
			var myFlashbox:Flashbox = myConnection.addFlashbox(flashBox);		
			
			//Adds the container to the stage
			this.addChild( myFlashbox );
			//stage.addChild(myFlashbox);
			myFlashbox.defaultXML = defaultContent;
			myFlashbox.addOnLoad(consumeXML);				
			myFlashbox.maxResponseTime = 10000;
			//myFlashbox.addMboxParameter('size',stageSize);				
			myFlashbox.load();
			
			ExternalInterface.call( "console.log" , "Got past load");			
			
			// CONSUME TEST AND TARGET RESPONSE, LOAD DIFFERENT CREATIVE, ETC.
			
			 function consumeXML(xmlResponse:XML):void {	
				var logo:String = xmlResponse.logo_overlay.toString();	
				var x:Number = xmlResponse.logo_x;
				var y:Number = xmlResponse.logo_y;			
				
				ExternalInterface.call( "console.log" , "XML Response=" + xmlResponse);	
				ExternalInterface.call( "console.log" , "logo=" + logo);	
				ExternalInterface.call( "console.log" , "x=" + x);	
				ExternalInterface.call( "console.log" , "y=" + y);	
				bug(logo,x,y);
				
			}
			
		}
		
		
		/*protected function initPlayer():void
		{
			//the pointer to the media
			var resource:URLResource = new URLResource( PROGRESSIVE_PATH );
			
			// Create a mediafactory instance
			mediaFactory = new DefaultMediaFactory();
			
			//creates and sets the MediaElement (generic) with a resource and path
			var element:MediaElement = mediaFactory.createMediaElement( resource );
			
			
			//the simplified api controller for media
			player = new MediaPlayer( element );
			
			//Marker 5: Add MediaPlayer listeners for media size and current time change
			player.addEventListener( DisplayObjectEvent.MEDIA_SIZE_CHANGE, _onSizeChange );
			player.addEventListener( TimeEvent.CURRENT_TIME_CHANGE, _onProgress );
			
			//the container (sprite) for managing display and layout
			container = new MediaContainer();
			container.addMediaElement( element );
			
			//Marker 1: Add Bug Overlay
			var bugUrlResource:URLResource = new URLResource( "assets/osmf_stacked.png" );
			var bug:ImageElement = new ImageElement( bugUrlResource );
			var layoutData:LayoutMetadata = new LayoutMetadata();
			layoutData.right = 170;
			layoutData.bottom = 510;
			layoutData.scaleMode = ScaleMode.NONE;
			bug.metadata.addValue( LayoutMetadata.LAYOUT_NAMESPACE, layoutData );
			var bugLoadTrait:LoadTrait = bug.getTrait( MediaTraitType.LOAD ) as LoadTrait; 
			bugLoadTrait.load();
			container.addMediaElement( bug );
			
			//Adds the container to the stage
			this.addChild( container );
		}*/
		
		
		protected function bug(logo,x,y):void{
			var resource:URLResource = new URLResource( PROGRESSIVE_PATH );
			ExternalInterface.call( "console.log" , "movie=" + PROGRESSIVE_PATH);
			
			// Create a mediafactory instance
			mediaFactory = new DefaultMediaFactory();
			
			//creates and sets the MediaElement (generic) with a resource and path
			var element:MediaElement = mediaFactory.createMediaElement( resource );
			ExternalInterface.call( "console.log" , "resource=" + resource);
			ExternalInterface.call( "console.log" , "element=" + element);
			
			//the simplified api controller for media
			player = new MediaPlayer( element );
			
			//Marker 5: Add MediaPlayer listeners for media size and current time change
			player.addEventListener( DisplayObjectEvent.MEDIA_SIZE_CHANGE, _onSizeChange );
			player.addEventListener( TimeEvent.CURRENT_TIME_CHANGE, _onProgress );
			
			//the container (sprite) for managing display and layout
			container = new MediaContainer();
			container.addMediaElement( element );
			ExternalInterface.call( "console.log" , "container=" + container);	
			
			//Marker 1: Add Bug Overlay
			var bugUrlResource:URLResource = new URLResource( logo );				
			var bug:ImageElement = new ImageElement( bugUrlResource );
			ExternalInterface.call( "console.log" , "bug overlay=" + bug);
			var layoutData:LayoutMetadata = new LayoutMetadata();
			layoutData.right = x;
			ExternalInterface.call( "console.log" , "x=" + layoutData.right);	
			layoutData.bottom = y;
			ExternalInterface.call( "console.log" , "y=" + layoutData.bottom);	
			layoutData.scaleMode = ScaleMode.NONE;
			bug.metadata.addValue( LayoutMetadata.LAYOUT_NAMESPACE, layoutData );
			var bugLoadTrait:LoadTrait = bug.getTrait( MediaTraitType.LOAD ) as LoadTrait; 
			bugLoadTrait.load();
			container.addMediaElement( bug );
			
			ExternalInterface.call( "console.log" , "container2=" + container.addMediaElement);	
			//Adds the container to the stage
			this.addChild( container );
			
		}
			
		/**
		 * Method to layout control bar. Source is a SWC in the /lib folder
		 * Instances are basic MovieClip symbols set to export for ActionScript
		 */
		protected function initControlBar():void
		{
			controlBar = new Sprite();
			
			controlBG = new ControlBG();
			
			pauseButton = new PauseButton();
			pauseButton.y = 2;
			pauseButton.useHandCursor = true;
			pauseButton.buttonMode = true;
			pauseButton.addEventListener( MouseEvent.CLICK, _onPauseClick );
			
			playButton = new PlayButton();
			playButton.y = 2;
			playButton.x = 43;
			playButton.useHandCursor = true;
			playButton.buttonMode = true;
			playButton.addEventListener( MouseEvent.CLICK, _onPlayClick );
			
			progressBG = new ProgressBG();
			progressBG.y = 2;
			progressBG.x = 87;
			progressBG.useHandCursor = true;
			progressBG.buttonMode = true;
			progressBG.addEventListener( MouseEvent.CLICK, _onSeek );
			
			progressCurrent = new ProgressCurrent();
			progressCurrent.y = 2;
			progressCurrent.x = 87;
			progressCurrent.mouseEnabled = false;
			
			this.addChild( controlBar );
			controlBar.addChild( controlBG );
			controlBar.addChild( pauseButton );
			controlBar.addChild( playButton );
			controlBar.addChild( progressBG );
			controlBar.addChild( progressCurrent );
		}
		
		/**
		 * Called whenever the media player dispatches that the current media resource has changed its size
		 * 
		 * @param p_evt	Contains the newHeight property used to reposition the controlbar
		 * 
		 */
		protected function _onSizeChange( p_evt:DisplayObjectEvent ):void
		{
			//Marker 6: Reposition appropriately when the media size changes	
			controlBar.y = p_evt.newHeight;
		}
		
		/**
		 * Called whenever the media player dispatches that the current media resource has changed its currentTime
		 * Determines the percent played and sets the current progress bar scalex to match
		 * 
		 * @param p_evt	Contains the time property used to determine the current time
		 * 
		 */
		protected function _onProgress( p_evt:TimeEvent ):void
		{	
			//Marker 7: Set the scalex of the current progress bar to the current time divided by the duration of the media	
			progressCurrent.scaleX = p_evt.time / player.duration;
		}
		
		
		
		//USER EVENT HANDLERS
		protected function _onPauseClick( p_evt:MouseEvent ):void
		{
			//Marker 3: Add call to pause method
			player.pause();
		}
		
		
		protected function _onPlayClick( p_evt:MouseEvent ):void
		{
			//Marker 4: Add call to pause method
			player.play();
		}
		
		/**
		 * Tell the media player to seek to time determined by dividing the mouse x-position 
		 * of the seek click by the total width of the progress track multiplied by the total duration
		 * 
		 * @param p_evt
		 * 
		 */
		protected function _onSeek( p_evt:MouseEvent ):void
		{
			//Marker 8: Call the seek method of the MediaPlayer passing it the appropriate time determined by the click on the progress track
			var seekTo:Number = player.duration * ( p_evt.target.mouseX/p_evt.target.width );
			player.seek( seekTo );
		}
	}
}